/**
 * loading动画
 */
const loadingAnimation = (function (doc) {
	let _render = null;
	// 渲染
	const render = function () {
		const loading = doc.createElement('div');
		loading.id = 'request-loading';
		loading.innerHTML = 'Loading...';
		loading.style.cssText =
			'z-index: 999; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 30px;';
		const mask = doc.createElement('div');
		mask.id = 'request-loading-mask';
		mask.style.cssText =
			'z-index: 998; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.05);';
		doc.body.appendChild(loading);
		doc.body.appendChild(mask);
		return {
			clear: function () {
				doc.body.removeChild(loading);
				doc.body.removeChild(mask);
			},
		};
	};

	// 显示
	const show = function () {
		if (_render) return;
		_render = render();
	};

	// 隐藏
	const hide = function () {
		_render && _render.clear();
		_render = null;
	};

	return { hide, show };
})(document);

export default loadingAnimation;
